package com.lcredu.lcrliving.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lcredu.common.util.PageUtils;
import com.lcredu.common.util.R;
import com.lcredu.lcrliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.lcredu.lcrliving.commodity.service.AttrAttrgroupRelationService;
import com.lcredu.lcrliving.commodity.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lcredu.lcrliving.commodity.entity.AttrEntity;
import com.lcredu.lcrliving.commodity.service.AttrService;

import javax.annotation.Resource;


/**
 * 商品属性表
 *
 * @author lcr
 * @email lcr@gmail.com
 * @date 2023-11-19 16:10:33
 */
@RestController
@RequestMapping("commodity/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Resource
    private CategoryService categoryService;

    /**
     * 1.新接口
     * 2.根据categoryId+查询条件key完成分页检索基本属性
     * 列表
     */
    @RequestMapping("/base/list/{categoryId}")
//    @RequiresPermissions("commodity:attr:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("categoryId") Long categoryId) {
//        PageUtils page = attrService.queryPage(params);
        PageUtils page = attrService.queryBasePage(params, categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 1.新接口
     * 2.根据categoryId+查询条件key完成分页检索销售属性
     * 列表
     */
    @RequestMapping("/sale/list/{categoryId}")
//    @RequiresPermissions("commodity:attr:list")
    public R saleAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("categoryId") Long categoryId) {
//        PageUtils page = attrService.queryPage(params);
        PageUtils page = attrService.querySalePage(params, categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
//    @RequiresPermissions("commodity:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrEntity attr = attrService.getById(attrId);

        Long categoryId = attr.getCategoryId();

        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(categoryId);

        attr.setCascadedCategoryId(cascadedCategoryId);
        System.out.println("商品号为" + attr.getCascadedCategoryId());
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("commodity:attr:save")
    public R save(@RequestBody AttrEntity attr) {
        System.out.println("attr" + attr);
        //attrService.save(attr);增加相对应的方法进行属性的存储
        attrService.saveAttrAndRelation(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("commodity:attr:update")
    public R update(@RequestBody AttrEntity attr) {
        attrService.updateById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("commodity:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
